package com.tmall.tmall.controller.admin;

import com.tmall.tmall.mapper.ProductMapper;
import com.tmall.tmall.pojo.*;
import com.tmall.tmall.service.OrderService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productOrder")
public class AdminOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductMapper productMapper;
    
    // OPid class for delivery and confirm operations
    @Data
    public static class OPid {
        private Integer oid;
        private Integer pid;
    }
    
    @GetMapping("/detail/{oid}")
    public Result getOrderDetail(@PathVariable Integer oid) {
        OrdersRt order = orderService.getOrderDetail(oid);
        return Result.success(order);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Orders orders){
        orderService.insert(orders);
        return Result.success();
    }
    @PutMapping("/update")
    public Result update(@RequestBody Orders orders){
        Orders order = orderService.getById(orders);
        orderService.update(orders);
        return Result.success();
    }
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String username,String name,String tel,String address,Integer status,Integer qwe){
        Integer uid=null;
        if(qwe!=null)uid= ThreadLocalUtils.getUid();
        PageRt list = orderService.page(pageNum,pageSize,username,name,tel,address,status,uid);
        return Result.success(list);
    }
    @DeleteMapping("/delBatch")
    public Result delete(@RequestBody List<Integer> ids){
        orderService.delete(ids);
        return Result.success();
    }
    @PostMapping("/pay/{oid}")
    public Result pay(@PathVariable Integer oid){
        orderService.pay(oid);
        return Result.success();
    }
    @PostMapping("/delivery")
    public Result delivery(@RequestBody OPid opId){
        orderService.delivery(opId.getOid(),opId.getPid());
        return Result.success();
    }
    @PostMapping("/confirm")
    public Result confirm(@RequestBody OPid opId){
        orderService.confirm(opId.getOid(),opId.getPid());
        return Result.success();
    }
    @PostMapping("/cancel/{oid}")
    public Result cancel(@PathVariable Integer oid){
        orderService.cancel(oid);
        return Result.success();
    }
}
